(function(factory) {
	if (typeof define === "function" && define.amd) {
		// AMD. Register as an anonymous module.
		define(factory);
	} else {
		// Browser globals
		factory();
	}
}(function() {
	/**
	 * @author 李球 <service@hteos.com>
	 * @class HteOS.Overview 应用全局管理组件
	 * @singleton
	 */
	HteOS.Overview = {
		
		rendered : false,
		/**
		 * 初始化全局管理组件
		 */
		init: function() {
			var me = this;
/*							
				$("<div class=\"hte-overview\" >" +
				"<div class=\"hte-overview-header\">" + 
				"        <h1>" + 
				"          应用管理" + 
				"           <span class=\"metro-icon-arrow-down-3 hte-overview-hide\" style=\"margin-top: 2px;\"></span>" + 
				"        </h1>" + 
				"      </div>" + 
				"      <div class=\"hte-overview-body\">" + 
				"      </div>" + 
				"    </div>").appendTo(".hte-metro");

	*/		
			var el = me.getEl();
			
			var html = "<%for(i = 0; i < groups.length; i ++) {%>" + 
							"<%var group = groups[i],apps = group.apps;%>" + 
					        "<div class=\"hte-overview-group hte-overview-item\"><%=group.name%></div>" + 
					        "<%for(j = 0; j < apps.length; j ++) {%>" + 
					        	"<div id=\"hte-overview-item-<%=apps[j].id%>\" data-app=\"<%=apps[j].id%>\" class=\"hte-overview-app hte-overview-item\">" 
					        		+ "<div class=\"hte-overview-app-icon\"><img src=\"<%=apps[j].icon%>\"></div>"
					        		+"<div class=\"hte-overview-app-name\"><%=apps[j].name%></div>" +
					        	"</div>" + 
					        "<%}%>" + 
					    "<%}%>";
			
			this.tpl =  HteOS.Template.compile(html);
			$(document).on("click.hte.overview.api", ".hte-overview-app", function(event) {
				var overview = $(this);
				HteOS.dock.DockManager.clear();
				HteOS.TaskManager.start(HteOS.AppManager.get(overview.data("app")));
				event.stopPropagation();
			});
			
			/*HteOS.EventManager.on("hte.app.load",function(){
				me.renderFlag = true;
				me.render();
			});*/
			me.render();
			
			//el.perfectScrollbar();
			
			HteOS.EventManager.on("hte.app.uninstall",function(app){
				me.remove(app)
			});
			
			HteOS.EventManager.on("hte.app.move",function(){
				me.render();
			});
			
			//监听点击事件
			HteOS.EventManager.on('hte.app.install', 
				function() {
					me.render();
			});
			
			//监听点击事件
			HteOS.EventManager.on('hte.group.sort', 
				function() {
					me.render();
			});
			
			//监听点击事件
			HteOS.EventManager.on('hte.group.rename', 
				function() {
					me.render();
			});
			
			me.initContextMenu();
		},
		
		render : function(){
			var me = this;
			var groups = this.collect();
			var html = this.tpl({
				groups : groups
			});
			me.getEl().find(".hte-overview-content").html(html);
			me.rendered = true;
		},
		
		collect : function(){
			var groups = HteOS.AppManager.groups;
			var result = [];
			for(var a in groups){
				if(groups[a].isEmpty !== true){
					var group = {};
					group.name = groups[a].name;
					group.index = groups[a].index;
					var apps = groups[a].apps;
					if(apps){
						var array = [];
						for(var id in apps){
							array.push(apps[id]);
						}
						array.sort(function(a,b){
							if(a.index > b.index){
								return 1;
							}
							return -1;
						});
						group.apps = array;
						result.push(group);
					}
				}
			}
			result.sort(function(a,b){
				if(a.index > b.index){
					return 1;
				}
				return -1;
			});
			return result;
		},

		getEl: function() {
			return $(".hte-overview");
		},

		remove :function(app){
			$("#hte-overview-item-" + app.id).remove();
			//this.layout();
		},

		initContextMenu: function() {
			new HteOS.menu.Menu({
				selector: ".hte-overview-app",
				cls: "hte-overview-contextmenu",
				items: [{
					id: 'open',
					icon: 'glyphicon glyphicon-new-window',
					name: '打开',
					handler: function() {
						var overview = $(".hte-overview-app.selected");
						HteOS.TaskManager.start(HteOS.AppManager.get(overview.data("app")));
						HteOS.Overview.hide();
					}
				}, {
					id: 'show',
					icon: 'glyphicon glyphicon-eye-open',
					name: '显示',
					handler: function() {
						var overview = $(".hte-overview-app.selected");
						HteOS.AppManager.show(HteOS.AppManager.get(overview.data("app")));
					}
				}, {
					id: 'hide',
					icon: 'glyphicon glyphicon-eye-close',
					name: '隐藏',
					handler: function() {
						var overview = $(".hte-overview-app.selected");
						HteOS.AppManager.hide(HteOS.AppManager.get(overview.data("app")));
					}
				}, '-', {
					id: 'uninstall',
					icon: 'glyphicon glyphicon-trash',
					name: '卸载',
					handler: function() {
						var overview = $(".hte-overview-app.selected");
						HteOS.AppManager.uninstall(HteOS.AppManager.get(overview.data("app")));
					}
				}],
				events: {
					show: function(target) {
						var me = this,
							tile;
						target = $(target);
						if (!target.hasClass("hte-overview-app")) {
							target = target.parent(".hte-overview-app");
						}
						$(target).addClass("selected");
						var app = HteOS.AppManager.get($(target).data("app"));
						if (app) {
							me[app.path ? 'enable' : 'disable']('open');
							if (app.hidden) {
								me.disable("hide");
								me.enable("show");
							} else {
								me.enable("hide");
								me.disable("show");
							}

							if (app.isNative) {
								me.disable("uninstall");
							} else {
								me.enable("uninstall");
							}
						}
					},
					hide: function() {
						$(".hte-overview-app.selected").removeClass("selected");
					}
				}
			});
		}
	};
}));